package org.jeecg.modules.ws.util;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;

/**
 * @author ZJJ
 * @date 2025-04-22 下午4:43
 * @copyright Copyright (c) 2025 ZJJ
 */
public class WyzlUtil {
    static public String sendHttpRequest(String ipAdress, int nPort, String sRequest){
        String sResult = "";
        OutputStream out = null;
        BufferedReader in = null;
        try{
            String encoding = "GB18030";	//如系统使用UTF-8编码，需将请求报文转码为GB18030
            String params = "requestXml=" + sRequest;	//HTTP请求需将请求报文放在requestXml参数发送

            String path = "http://" + ipAdress + ":" + nPort;
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            conn.setConnectTimeout(10 * 1000);	 //设置连接超时时间

            conn.setDoOutput(true);		//将 doOutput 标志设置为 true，指示应用程序要将数据写入 URL 连接。
            conn.setDoInput(true);		////将 doInput 标志设置为 true，指示应用程序要从 URL 连接读取数据。

            //设置HTTP请求报文头
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length", String.valueOf(params.length()));
            conn.setRequestProperty("Connection", "close");

            //发送请求报文数据
            out = conn.getOutputStream();
            out.write(params.getBytes(encoding));
            out.flush();
            out.close();

            //读取返回数据
            if (conn.getResponseCode() == 200){
                in = new BufferedReader(new InputStreamReader(conn.getInputStream(), encoding));
            }else{
                in = new BufferedReader(new InputStreamReader(conn.getErrorStream(), encoding));
            }

            String sLine = null;
            StringBuffer sb = new StringBuffer();
            while((sLine = in.readLine()) != null){
                sb.append(sLine);
            }
            sResult = sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if(in!=null)
                    in.close();
                if(out!=null)
                    out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sResult;
    }

    static public String sendSocketRequest(String ipAddress, int nPort, String sRequest){
        String sResult = "";
        try {
            Socket socket = new Socket(ipAddress, nPort);
            BufferedReader ins = new BufferedReader(new InputStreamReader(socket.getInputStream(), "GB18030"));
            PrintWriter outs = new PrintWriter(socket.getOutputStream());

            outs.print(sRequest);
            outs.flush();
            socket.shutdownOutput();

            String sLine = null;
            StringBuffer sb = new StringBuffer();
            while((sLine = ins.readLine()) != null){
                sb.append(sLine);
            }

            ins.close();
            outs.close();
            socket.close();

            return sb.toString();

        } catch (NumberFormatException e) {
            sResult = e.getMessage();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            sResult = e.getMessage();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            sResult = e.getMessage();
        }
        return sResult;
    }
}
